Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expectation Over Transformation Wrapper #719

Merged
merged 4 commits into from
Mar 4, 2024
Merged

Conversation

SerezD
Copy link
Contributor

@SerezD SerezD commented Feb 26, 2024

Closes #563 and #191

My approach for implementing EoT.
I tried to follow the suggestions of @zimmerrol.

Basically, I created a new ExpectationOverTransformation Wrapper class that computes the forward pass n_steps times (default to 16) and returns the average prediction.

I also created a new example for pytorch resnet-18 model, which seems to work.

Potential Issue: since the forward pass is executed n_steps times, the attack takes a long time for execution.

@zimmerrol
Copy link
Member

Great, thanks for preparing this! Would you mind adding unit tests for the new module, too?

@SerezD
Copy link
Contributor Author

SerezD commented Feb 26, 2024

By looking at the other test files, I have added some tests to assert that wrapping the model with EoT does not change clean / robust accuracy (when no random transforms are applied). Don't know if this is correct.

Also, I am not really sure how to add more complex tests (with random transformations applied to the input).

Copy link
Member

@zimmerrol zimmerrol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except a few smaller things, this looks good to me! But please make sure that all tests listed at the end of the page pass; otherwise, we sadly cannot merge the PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename this file to something like test_eot_wrapper.py.

from conftest import ModeAndDataAndDescription


def test_eot_attack(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above.

epsilons = (5000.0, L2(50.), Linf(1.))

for attack, eps in zip(attacks, epsilons):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary whitespace.

@SerezD
Copy link
Contributor Author

SerezD commented Mar 1, 2024

I have uploaded a refactored version, which should now be compatible with black and mypy requirements. This should fix the failed checks for the Tests part.

@codecov-commenter
Copy link

codecov-commenter commented Mar 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.47%. Comparing base (bb56af1) to head (0f3c7cf).

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #719   +/-   ##
=======================================
  Coverage   98.46%   98.47%           
=======================================
  Files          56       56           
  Lines        3529     3546   +17     
=======================================
+ Hits         3475     3492   +17     
  Misses         54       54           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@coveralls
Copy link

coveralls commented Mar 2, 2024

Coverage Status

coverage: 98.477% (+0.007%) from 98.47%
when pulling 0f3c7cf on SerezD:master
into bb56af1 on bethgelab:master.

Copy link
Member

@zimmerrol zimmerrol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one minor comment left, then we can merge this!

@@ -21,3 +21,29 @@ def __call__(self, inputs: T) -> T:
y = ep.where(x < self._threshold, min_, max_).astype(x.dtype)
z = self._model(y)
return restore_type(z)


class ExpectationOverTransformationWrapper(Model):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not mentioning this earlier, but can you please add docstrings to the class & module like its done for the other classes in the project?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added it to the models.rst file, taking the other wrapper module as an example.
However, I am not sure how to fix the version error for sphinx. In my local machine, I tried to build html docs with sphinx version 5 and it works, but the requirements.txt file in the docs folder wants:

sphinx==4.5.0
sphinx-autobuild==2021.3.14
sphinx_rtd_theme==1.0.0
sphinx-typlog-theme==0.8.0

@zimmerrol zimmerrol merged commit 55d2c8d into bethgelab:master Mar 4, 2024
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DifferentiableModelWrapper equivalent for Expectation Over Transformation?
4 participants